<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        input{
            width: 141px;
            height: 28px;
            margin-top: 30px;
        }

    </style>
</head>
<body>
    <div id="root">
        <input type="text" placeholder="请输入关键字查找" v-model="inputValue">
        <button @click="sortType=1">升序</button>
        <button @click="sortType=2">降序</button>
        <button @click="sortType=0">原顺序</button>
        <ul>
            <li v-for='p in computedPeople' :key="p.id">{{p.name}}-{{p.age}}</li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        let vm = new Vue({
            el: '#root',
            data: {
                peoples:[
                    {id:01,name:'马冬梅',age:30},
                    {id:02,name:'周冬雨',age:19},
                    {id:03,name:'周杰伦',age:24},
                    {id:04,name:'温兆伦',age:21},
                ],
                inputValue:'',
                // 0表示原顺序， 1表示升序， 2表示降序
                sortType:0,
            },
            computed: {
                computedPeople(){
                    // 筛选功能
                    const arr = this.peoples.filter((p) => {
                        return p.name.includes(this.inputValue);
                    });

                    // 排序功能
                    // 判断排序类型
                    if (this.sortType){
                        // 给数组arr按照年龄排序
                        arr.sort((p1, p2) => {
                            return this.sortType==1 ? p1.age-p2.age : p2.age-p1.age; 
                        });
                    };
                    return arr;
                },
            }
        });
    </script>
</body>
</html>